<?php
/**
 * Created by PhpStorm.
 * FileName: ConsultController.class.php
 * User: Administrator
 * Date: 2018/5/22
 * Time: 14:55
 */

namespace Home\Controller;
use Think\Controller;
use Admin\Model\ConsultingModel;
use Admin\Model\ReplyModel;
/**
 * 服务咨询
 * Class ConsultController
 * @package WeiXin\Controller
 */
class ConsultController extends Controller {
    private $consult;
    private $reply;
    function __construct() {
        $this->consult =  new ConsultingModel();
        $this->reply =  new ReplyModel();
        parent::__construct();
    }

    public function index() {
		$type = I('type');
        if($type == 1){
			$this->assign('pageTitle','服务咨询');
			$this->assign('ganni','咨询');
		}else{
			$this->assign('pageTitle','问题投诉');
			$this->assign('ganni','投诉');
		}
		$this->assign('type',$type);
        $this->display();
    }

    public function add() {
		$type = I('type');
		if($type == 1){
			$this->assign('pageTitle','服务咨询');
			$this->assign('ganni','咨询');
		}else{
			$this->assign('pageTitle','问题投诉');
			$this->assign('ganni','投诉');
		}
        $this->assign("type",$type);
        $this->display();
    }
    public function detail() {
        $id = I('id');
        $consult = $this->consult->where(array('id' => $id))->find();
        $reply = $this->reply->where(array('consult_id' => $id))->order('reply_time ASC')->select();
        if(!empty($reply)){
            foreach ($reply as $k=>$v){
                $reply[$k]['avatar'] = getAvatar($v['reply_id']);
                $reply[$k]['replyName'] = getUserName($v['reply_id']);
            }
        }
        $this->assign('consult',$consult);
        $this->assign('reply',json_encode($reply));
        $this->assign('pageTitle','咨询详情');
        $this->assign('consultId',$id);
        $this->display();
    }
	
	public function doAdd() {
        $userId = 0;
        $title = $_POST['title'];
        $content = $_POST['content'];
		$type = I('type');
        if (!$title || !$content) {
            return $this->ajaxReturn(['msg' => "无效的参数！",
    		'code' => -100,
    		'data' => ''
			
			]);
        }

        $data = array(
            'title' => $title,
            'content'=> $content,
            'consult_id'=> $userId,
            'consult_time'=> time(),
			'type' => $type,
             'status'=> 1
        );
        $rs = $this->consult->add($data);

        if ($rs !== false) {
            $res= $this->consult->where(array('id' => $rs))->find();
            //$this->sendConsultNotice($res);//发送咨询通知
            //$this->sendManageNotice($res);//给工作管理员发送咨询通知
            return $this->ajaxReturn(
			[
				'msg' => "咨询成功！",
				'code' => 200,
				'data' => ''
			]);
        }
        return $this->ajaxReturn([
				'msg' => "咨询失败！",
				'code' => -100,
				'data' => ''
			]);
    }
	
	public function getConsultList(){
        $curPage = I('p',1);
		$type = I('type');
        $where = [
			'type' => $type,
			'is_delete' => ['neq',1]
		];
        $result =  $this->consult->getApiConsulList($where,$curPage,50);
        $data['data'] = $result;
        $data['curPage'] = $curPage+1;
        $data['totalPage'] = $result['totalPage'];
        return $this->ajaxReturn([
				'msg' => "资讯列表",
				'code' => 200,
				'data' => $data]
				);
    }
}